//time_helper.c
#define TIME_HELPER_C

#include "time_helper.h"


//Gets the current formatting time

int get_rand(){
    srand((unsigned int)time(NULL));
    int random_number = rand();
    return random_number;
}



void get_format_time(void){
    time_t now_time;
    struct tm *time_info;
    char strinfo[50];
    time_info = localtime(&now_time);
    // 格式化时间字符串
    strftime(strinfo, sizeof(strinfo), "%Y-%m-%d %H:%M:%S", time_info);
    // 打印时间
    printf("Current local time and date: %s\n",strinfo);
}



//处理器时钟滴答数 (处理器时钟滴答数表示处理器执行的时钟周期数。)
clock_t time_difference_CT(void (*fptr)(void)){
    clock_t start_time,end_time;
    struct tm *start_time_info,*end_time_info;
    char start_strinfo[80],end_strinfo[80];

    start_time = clock();
    
    fptr();
    
    end_time = clock();
    
    printf("Time difference:%ld (clock ticks)\n",end_time-start_time);
}


// 使用高精度计数器 （仅适用于Windows系统）
double time_difference_Win(void (*fptr)(void)) {
    LARGE_INTEGER start_time, end_time, frequency;
    double elapsed_time;
    // 获取高精度计数器的频率
    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start_time);

    fptr();

    QueryPerformanceCounter(&end_time);
    // 计算时间差（微秒）
    elapsed_time = (double)(end_time.QuadPart - start_time.QuadPart) / frequency.QuadPart;
    printf("Time difference: %.9f seconds\n", elapsed_time);
}